<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索输入框 -->
      <div style="margin-top: 1px">
        <el-input placeholder="请输入内容" v-model="input2">
          <el-button
            @click="searchGoods"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addGoods">添加商品</el-button>
      </div>
      <!-- 商品数据列表 -->
      <el-table
        ref="singleTable"
        :data="list"
        border
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" width="50">
          <template slot-scope="{ $index }">
            {{ obj.pagesize * (obj.pagenum - 1) + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column property="goods_name" label="商品名称" width="600">
        </el-table-column>
        <el-table-column
          property="goods_price"
          label="商品价格(元)"
          width="150"
        >
        </el-table-column>
        <el-table-column property="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column property="add_time" label="创建时间" width="200">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              @click="editHandle(row)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              @click="del(row.goods_id)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getGoodsAPI, delGoods } from '@/api/goods'
export default {
  name: '',
  data () {
    return {
      obj: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      list: [],
      total: 0,
      input2: '',
    }
  },
  created () {
    this.getGoods()
  },
  computed: {
  },
  methods: {
    async getGoods () {
      const res = await getGoodsAPI(this.obj)
      // console.log(res, 'goods');
      this.list = res.data.goods
      this.total = res.data.total
      // console.log(this.list, 'list');
      this.$message({
        type: 'success',
        message: '获取商品列表成功!'
      });
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val
      this.getGoods()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val
      this.getGoods()
    },
    editHandle () { },
    del (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //b表示点击的是确定按钮
        await delGoods(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getGoods()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    async searchGoods () {
      console.log(this.input2);
      this.obj.query = this.input2
      this.obj.pagenum = 1
      const res = await getGoodsAPI(this.obj)
      console.log(res);
      this.getGoods()
    },
    addGoods () {
      this.$router.push('/goods/add')
    },
  }
}
</script>
<style lang='less'  scoped>
.el-form {
  width: 90%;
}
.box-card {
  margin-top: 15px;
  .el-input {
    width: 500px;
    margin-right: 20px;
  }
}
</style>
